package com.ruoyi.project.system.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysStock;
import com.ruoyi.project.system.domain.vo.InAndOutVo;
import com.ruoyi.project.system.service.ISysStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 仓储货物Controller
 *
 * @author orange
 * @date 2022-04-07
 */
@RestController
@RequestMapping("/system/stock")
public class SysStockController extends BaseController {
  @Autowired private ISysStockService sysStockService;

  /** 查询仓储货物列表 */
  @PreAuthorize("@ss.hasPermi('system:stock:list')")
  @GetMapping("/list")
  public TableDataInfo list(SysStock sysStock) {
    startPage();
    List<SysStock> list = sysStockService.selectSysStockList(sysStock);
    return getDataTable(list);
  }

  /** 导出仓储货物列表 */
  @PreAuthorize("@ss.hasPermi('system:stock:export')")
  @Log(title = "仓储货物", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HttpServletResponse response, SysStock sysStock) {
    List<SysStock> list = sysStockService.selectSysStockList(sysStock);
    ExcelUtil<SysStock> util = new ExcelUtil<SysStock>(SysStock.class);
    util.exportExcel(response, list, "仓储货物数据");
  }

  /** 获取仓储货物详细信息 */
  @PreAuthorize("@ss.hasPermi('system:stock:query')")
  @GetMapping(value = "/{stockId}")
  public AjaxResult getInfo(@PathVariable("stockId") Long stockId) {
    return AjaxResult.success(sysStockService.selectSysStockByStockId(stockId));
  }

  /** 新增仓储货物 */
  @PreAuthorize("@ss.hasPermi('system:stock:add')")
  @Log(title = "仓储货物", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@RequestBody SysStock sysStock) {
    return toAjax(sysStockService.insertSysStock(sysStock));
  }

  /** 修改仓储货物 */
  @PreAuthorize("@ss.hasPermi('system:stock:edit')")
  @Log(title = "仓储货物", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@RequestBody SysStock sysStock) {
    return toAjax(sysStockService.updateSysStock(sysStock));
  }

  /** 删除仓储货物 */
  @PreAuthorize("@ss.hasPermi('system:stock:remove')")
  @Log(title = "仓储货物", businessType = BusinessType.DELETE)
  @DeleteMapping("/{stockIds}")
  public AjaxResult remove(@PathVariable Long[] stockIds) {
    return toAjax(sysStockService.deleteSysStockByStockIds(stockIds));
  }

  @PreAuthorize("@ss.hasPermi('system:stock:inAndOut')")
  @Log(title = "仓储货物", businessType = BusinessType.InAndOut)
  @PostMapping("/inAndOut")
  public AjaxResult inAndOutWarehouse(@RequestBody InAndOutVo sysStock) {
    return toAjax(sysStockService.inAndOutWarehouse(sysStock));
  }
}
